<?php

/**
 * XML Class
 *
 * @author Raymond Benc aka natio <www.raybenc.com>
 * @package Xml
 * @copyright phpFoX Limited
 */
class Xml
{
	/**
	 * Initiate XML class
	 * @param string $sPath Path to the XML file
	 * @return bool Checks if we were able to retrieve the data
	 */
	function Xml($sPath)
	{
		if( substr($sPath, 0, 7) == 'http://' )
		{
			if ( extension_loaded('curl') )
			{
				$ch = curl_init($sPath);
				$sCachePath = @cachedDir . "xml". time() .".xml";
				$fp = fopen($sCachePath, "w+");
				curl_setopt($ch, CURLOPT_FILE, $fp);
				curl_setopt($ch, CURLOPT_HEADER, 0);
				curl_exec($ch);
				curl_close($ch);
				fclose($fp);
				if ( file_exists($sCachePath) )
				{
					$sPath = $sCachePath;
				}
			}
		}

		if ( ($this->sData = @file_get_contents($sPath)) !== false )
		{
			if ( isset($sCachePath) )
			{
				@unlink($sCachePath);
			}
			return true;
		}
		return false;
	}

	/**
	 * Get date from feedburner.com
	 *
	 * @return array Details we picked up
	 */
	function getFeedBurner()
	{
		$parser = xml_parser_create();
		xml_parser_set_option($parser, XML_OPTION_CASE_FOLDING, 0);
		xml_parser_set_option($parser, XML_OPTION_SKIP_WHITE, 1);
		xml_parse_into_struct($parser, $this->sData, $values, $tags);
		xml_parser_free($parser);

		$arrQuotes = array();
		$tagOpen = false;

		foreach($values as $key => $item)
		{
    		if(!$tagOpen and $item['tag'] == 'item' and $item['type'] == 'open')
    		{
                $tagOpen = true;
                $temp_title = '';
        		$temp_description = '';
        		$temp_guid = '';
        		$temp_link = '';

    		}
    		elseif($item['tag'] == 'item' and $item['type'] == 'close')
    		{
    	        $tagOpen = false;
                if($temp_title != '' and $temp_description != '' and $temp_guid != '' and $temp_link != '')
        		{
                    $arrQuotes[] = array(
                		'title' => $temp_title,
                		'description' => $temp_description,
                		'guid' => $temp_guid,
                		'link' => $temp_link
            		);
		        }
    		}
    		else
    		{
				switch($item['tag'])
				{
            		case 'title':
                	$temp_title = $item['value'];
            		break;
            		case 'description':
            		case 'atom:summary':
                    $temp_description = (isset($item['value']) ? $item['value'] : '');
		            break;
        		    case 'guid':
                	$temp_guid = $item['value'];
            		break;
            		case 'link':
					$temp_link = $item['value'];
            		break;
            		default: break;
        		}
    		}
		}

		return $arrQuotes;
	}
}

?>